package com.zhrj.exam.controller;

import com.github.pagehelper.PageInfo;
import com.zhrj.exam.entity.BladeUser;
import com.zhrj.exam.service.AuthService;
import com.zhrj.exam.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.concurrent.TimeUnit;

@RestController
@RequestMapping("/user")
@Slf4j
public class UserController {

    @Resource
    private AuthService authService;

    @Resource
    private UserService userService;

    @Resource
    private RedisTemplate redisTemplate;

    @Resource
    private StringRedisTemplate stringRedisTemplate;


    @PostMapping("/access-token")
    public String accessToken(@RequestParam(defaultValue = "000000", required = false) String tenantId,
                              @RequestParam(name="username",required = false) String account,
                              @RequestParam(required = false) String password) {
        String authToken = authService.getAuthToken(tenantId, account, password);
        log.info(authToken);
        stringRedisTemplate.opsForValue().set("token:" + account, authToken,30, TimeUnit.DAYS);
        return authToken;
    }


    @GetMapping
    public List<BladeUser> getUserList(@RequestParam(name = "username",defaultValue = "admin")String account) {
        return userService.getUserInfo(account);
    }

    @GetMapping("/page")
    public PageInfo<BladeUser> getUserListForPage(@RequestParam(name = "pageNum",defaultValue = "1")Integer pageNum,
                                                  @RequestParam(name = "pageSize",defaultValue = "2")Integer pageSize) {
        return userService.getUserForPage(pageNum, pageSize);
    }


    @PostMapping("/update")
    public String updateUserInfo() {
        userService.updateUserInfo();
        return "success";
    }


    @PostMapping("/export")
    public String exportUsersToMinio() {
        userService.exportUsersToMinio();
        return "success";
    }

}
